//
//  HMStatusesViewModel.swift
//  Weibo
//
//  Created by 吴少飞 on 16/9/23.
//  Copyright © 2016年 wushaofei. All rights reserved.
//

import UIKit

class HMStatusesListViewModel: NSObject {

    lazy var statusesList:[HMStatusesViewModel] = [HMStatusesViewModel]()
    
    func loadData(callBack:@escaping (Bool)->()) {
        
        
        let accessToken = HMUserAccountViewModel.sharedUserAccountViewModel.accessToken!
        
        HMNetworkTools.sharedTools.requestStatuses(accessToken: accessToken, callBack: { (response, error) in
            
            if error != nil{
                
                print("网络异常\(error)")
                callBack(false)
                return
            }
            
            guard let dict = response as? [String:Any] else{
                
                callBack(false)
                return
            }
            guard let statusesArrry = dict["statuses"] as? [[String:Any]] else{
                
                callBack(false)
                return
            }
            let statusesArrryM = NSArray.yy_modelArray(with: HMStatuses.self, json: statusesArrry) as! [HMStatuses]
            
            
            var tempArray = [HMStatusesViewModel]()
            //  遍历模型数组
            for status in statusesArrryM {
                //  把模型转成viewModel让cell使用
                let viewModel = HMStatusesViewModel(status: status)

                tempArray.append(viewModel)
            }
            
            //  给数据源赋值
            self.statusesList = tempArray
            
            callBack(true)
        })
        
        
    }
}
